const { src, dest } = require('gulp');
const replace = require('gulp-replace');
const mergeJson = require('gulp-merge-json');
const jscrambler = require('gulp-jscrambler');
const rename = require("gulp-rename");
const args = require('args');
const inquirer = require("inquirer");
var through = require('through2');
const { projectMap } = require('./input/cwd');

function passingCallback() {
    return src('config.js').pipe(through.obj(function (file, encode, cb){
         // 文本内容转为字符串
         var result = file.contents.toString()
         // 添加了一点东西
        //  result += ' \n ' + 'let b = 5'
         // 再次转为Buffer对象，并赋值给文件内容
         file.contents = new Buffer(result)
         // 以下是例行公事
         this.push(file)
        console.log('result', result)
         cb()
    })).pipe(dest('output/'));
}

function task2() {
    return src('config.js').pipe(replace('广西区妇幼', '厉害的厉害的')).pipe(dest('output/'));
}

function task3() {
    return src('input/test.json').pipe(mergeJson({
        fileName: 'a.json',
        jsonSpace: '  ',
        transform: (json) => {
          json.appid = '123456789'
          return json
        },
      })).pipe(dest('output/'));
}

function enable(filesSrc) {
    if (filesSrc.length === 0) {
      return false;
    }
  
    return true;
  }

function task4() {
    return src('config.js').pipe(jscrambler({
        keys: {
          accessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
          secretKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        },
        applicationId: 'XXXXXXXXXXXX',
        params: [
          {
            name: 'whitespaceRemoval'
          },
          {
            name: 'stringSplitting'
          }
        ]
      }))
      .pipe(dest('output/'));
}

function task5() {
    return src('input/qfy.json').pipe(through.obj(function (file, encode, cb){
        // 文本内容转为字符串
        var result = file.contents.toString()
        // 添加了一点东西
        result = 'export default' + ' ' + result
        // 再次转为Buffer对象，并赋值给文件内容
        file.contents = new Buffer(result)
        // 以下是例行公事
        this.push(file)
        cb()
   })).pipe(rename('config.js')).pipe(dest('output/'));
}

function task6() {
    const promptList = [{
        type: 'rawlist',
        message: '请选择项目医院:',
        name: 'project',
        choices: projectMap
    }];
    inquirer.default.prompt(promptList).then(answers => {
        console.log(answers); // 返回的结果
        src(`input/${answers.project}.json`).pipe(through.obj(function (file, encode, cb){
            // 文本内容转为字符串
            var result = file.contents.toString()
            // 添加了一点东西
            result = 'export default' + ' ' + result
            // 再次转为Buffer对象，并赋值给文件内容
            file.contents = new Buffer(result)
            // 以下是例行公事
            this.push(file)
            cb()
       })).pipe(rename('config.js')).pipe(dest('output/'));
       src(`input/css/${answers.project}-global.less`).pipe(rename('global.less')).pipe(dest('output/'));
       return
    });
}



exports.default = task6;